1
|
|
|
import Handlebars from 'handlebars' |
2
|
|
|
import striptags from 'striptags' |
3
|
|
|
|
4
|
|
|
import { |
5
|
|
|
cmsData |
6
|
|
|
,config |
7
|
|
|
,cmsTemplates |
8
|
|
|
} from '../../' |
9
|
|
|
|
10
|
|
|
export function addAbeAttrSingleTab(key, elem, htmlAttribute = null) { |
11
|
|
|
var res = '' |
12
|
|
|
|
13
|
|
|
var valueOfAttritube = key.replace(/\./g, '-') |
14
|
|
|
key = cmsData.regex.validDataAbe(valueOfAttritube) |
15
|
|
|
|
16
|
|
|
if (htmlAttribute != null) { |
17
|
|
|
res = ' data-abe-attr-' + valueOfAttritube + '="' + htmlAttribute + '"' + ' data-abe-' + valueOfAttritube + '="' + key + '"' + elem |
18
|
|
|
}else { |
19
|
|
|
res = ' data-abe-' + key + '="' + key + '" ' + elem |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return res |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
export function addAbeAttrForBlock(key, elem, htmlAttribute = null) { |
26
|
|
|
var res = '' |
27
|
|
|
|
28
|
|
|
var valueOfAttritube = key.split('.') |
29
|
|
|
var parentKey = valueOfAttritube.shift() |
30
|
|
|
valueOfAttritube = `${parentKey}[index].${valueOfAttritube[0].replace(/\./g, '-')}` |
31
|
|
|
var valueOfAttritubeIndexed = valueOfAttritube.replace(/\[index\]/, '{{@index}}') |
32
|
|
|
key = cmsData.regex.validDataAbe(valueOfAttritube) |
33
|
|
|
|
34
|
|
|
if (htmlAttribute) { |
35
|
|
|
|
36
|
|
|
res = ` data-abe-attr-${valueOfAttritube}="${htmlAttribute}" data-abe-${valueOfAttritube}="${key}"` |
37
|
|
|
+ ` data-abe-attr-${valueOfAttritubeIndexed}="${htmlAttribute}" data-abe-${valueOfAttritubeIndexed}="${key}"${elem}` |
38
|
|
|
}else { |
39
|
|
|
res = ` data-abe-${valueOfAttritube}="${key}"` |
40
|
|
|
+ ` data-abe-${valueOfAttritubeIndexed}="${key}" ${elem}` |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return res |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* THIS: |
48
|
|
|
<span>{{abe type='text' key='text_visible'}}</span> |
49
|
|
|
|
50
|
|
|
* BECOME: |
51
|
|
|
<span data-abe-text_visible="text_visible" >{{abe type='text' key='text_visible'}}</span> |
52
|
|
|
|
53
|
|
|
* @param {[type]} template [description] |
54
|
|
|
*/ |
55
|
|
|
export function addAbeDataAttrForHtmlTag(template) { |
56
|
|
|
var match |
57
|
|
|
var key |
58
|
|
|
var getattr |
59
|
|
|
var newTemplate = template |
60
|
|
|
|
61
|
|
|
while (match = cmsData.regex.abePattern.exec(template)) { |
62
|
|
|
key = cmsData.regex.getAttr(match, 'key') |
63
|
|
|
|
64
|
|
|
if (!cmsData.regex.isSingleAbe(match, newTemplate)) { |
65
|
|
|
key = key.replace('.', '{{@index}}.') |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
getattr = key.replace(/\./g, '-') |
69
|
|
|
|
70
|
|
|
newTemplate = newTemplate.replace( |
71
|
|
|
cmsData.regex.escapeTextToRegex(match[0], 'g'), |
72
|
|
|
' data-abe-' + cmsData.regex.validDataAbe(getattr) + '="' + getattr + '" ' + match[0] |
73
|
|
|
) |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return newTemplate |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
export function addHasAbeAttr(text) { |
80
|
|
|
return text.replace('}}', ' has-abe=1}}') |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
export function getAbeAttributeData(match, text, htmlAttribute, abeTag) { |
84
|
|
|
var valueOfAttritube |
|
|
|
|
85
|
|
|
var key = cmsData.regex.getAttr(match, 'key') |
86
|
|
|
var res |
87
|
|
|
|
88
|
|
|
if (cmsData.regex.isSingleAbe(match, text)) { |
89
|
|
|
// valueOfAttritube = key.replace(/\./g, '-') |
90
|
|
|
// key = cmsData.regex.validDataAbe(valueOfAttritube) |
91
|
|
|
// key = key.replace(/\./g, '-') |
92
|
|
|
// res = ' data-abe-attr-' + valueOfAttritube + '="' + htmlAttribute + '"' + ' data-abe-' + valueOfAttritube + '="' + key + '"' + abeTag |
93
|
|
|
res = addAbeAttrSingleTab(key, abeTag, htmlAttribute) |
94
|
|
|
}else { |
95
|
|
|
res = addAbeAttrForBlock(key, abeTag, htmlAttribute) |
96
|
|
|
// valueOfAttritube = key.split('.') |
97
|
|
|
// var parentKey = valueOfAttritube.shift() |
98
|
|
|
// valueOfAttritube = `${parentKey}[index].${valueOfAttritube[0]}` |
99
|
|
|
// var valueOfAttritubeIndexed = valueOfAttritube.replace(/\[index\]/, '{{@index}}') |
100
|
|
|
// key = cmsData.regex.validDataAbe(valueOfAttritube) |
101
|
|
|
|
102
|
|
|
// res = ` data-abe-attr-${valueOfAttritube}="${htmlAttribute}" data-abe-${valueOfAttritube}="${key}"` |
103
|
|
|
// + ` data-abe-attr-${valueOfAttritubeIndexed}="${htmlAttribute}" data-abe-${valueOfAttritubeIndexed}="${key}"${abeTag}` |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return res |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
* IF ABE TAG SINGLE (NOT ABE EACH STATEMENT) |
112
|
|
|
* |
113
|
|
|
* THIS: |
114
|
|
|
<img src="{{abe type='image' key='image_key' tab='default'}}" alt=""> |
115
|
|
|
|
116
|
|
|
* BECOME: |
117
|
|
|
<img data-abe-attr-image_key="src" data-abe-image_key="image_key" data-abe-attr-image_key="src" |
118
|
|
|
data-abe-image_key="image_key" src="{{abe type='image' key='image_key' tab='default' has-abe=1 has-abe=1}}" alt=""> |
119
|
|
|
|
120
|
|
|
* |
121
|
|
|
* IF ABE EACH TAG |
122
|
|
|
* THIS: |
123
|
|
|
{{#each test}} |
124
|
|
|
<img src="{{abe type='image' key='test.img' desc='test_img' tab='default'}}" alt=""> |
125
|
|
|
{{/each}} |
126
|
|
|
|
127
|
|
|
* BECOME: |
128
|
|
|
{{#each test}} |
129
|
|
|
<img data-abe-attr-test[index].img="src" data-abe-test[index].img="test[index].img" src="{{abe type='image' key='test.img' desc='test_img' tab='default' has-abe=1}}" alt=""> |
130
|
|
|
{{/each}} |
131
|
|
|
|
132
|
|
|
* @param {[type]} template [description] |
133
|
|
|
*/ |
134
|
|
|
export function addAbeDataAttrForHtmlAttributes(template) { |
135
|
|
|
var text = template.replace(/<([A-Za-z]+)/g, '\nABE_SPLIT<$1') |
136
|
|
|
let abeTagIntoAttribute = text.match(cmsData.regex.abeAsAttributePattern) |
137
|
|
|
|
138
|
|
|
if (abeTagIntoAttribute != null) { |
139
|
|
|
Array.prototype.forEach.call(abeTagIntoAttribute, (abeIntoTag) => { |
140
|
|
|
let matchAbeTag = /({{abe.*?[\s\S].*?}})/g.exec(abeIntoTag) |
141
|
|
|
|
142
|
|
|
if(matchAbeTag != null && matchAbeTag[1] != null) { |
143
|
|
|
var toReplace = cmsTemplates.prepare.getAbeAttributeData(matchAbeTag[1], text, (abeIntoTag.split('=')[0]).trim(), abeIntoTag) |
144
|
|
|
|
145
|
|
|
toReplace = toReplace.replace( |
146
|
|
|
cmsData.regex.escapeTextToRegex(matchAbeTag[1]), |
147
|
|
|
cmsTemplates.prepare.addHasAbeAttr(matchAbeTag[1]) |
148
|
|
|
) |
149
|
|
|
|
150
|
|
|
text = text.replace( |
151
|
|
|
cmsData.regex.escapeTextToRegex(abeIntoTag), |
152
|
|
|
toReplace |
153
|
|
|
) |
154
|
|
|
} |
155
|
|
|
}) |
156
|
|
|
} |
157
|
|
|
text = text.replace(/\nABE_SPLIT</g, '<') |
158
|
|
|
|
159
|
|
|
return text |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Example: |
164
|
|
|
* |
165
|
|
|
* |
166
|
|
|
* THIS: |
167
|
|
|
{{abe type='data' key='data_key' source='select title from article' display='title' editable='true' tab='default'}} |
168
|
|
|
|
169
|
|
|
{{#each data_key}} |
170
|
|
|
{{title}} |
171
|
|
|
{{/each}} |
172
|
|
|
|
173
|
|
|
* |
174
|
|
|
* BECOME THIS |
175
|
|
|
|
176
|
|
|
{{abe type='data' key='data_key' source='select title from article' display='title' editable='true' tab='default'}} |
177
|
|
|
|
178
|
|
|
{{#each data_key}} |
179
|
|
|
{{title}} |
180
|
|
|
{{/each}}<!-- [[data_key]] %7B%7B%23each%20data_key%7D%7D%0A%09%7B%7Btitle%7D%7D%0A%7B%7B/each%7D%7D --> |
181
|
|
|
|
182
|
|
|
* @param {[type]} template [description] |
183
|
|
|
* @param {[type]} json [description] |
184
|
|
|
*/ |
185
|
|
|
export function addAbeSourceComment(template, json) { |
186
|
|
|
|
187
|
|
|
if(typeof json.abe_source !== 'undefined' && json.abe_source !== null) { |
188
|
|
|
var keys = Object.keys(json.abe_source) |
189
|
|
|
|
190
|
|
|
for(var i in keys) { |
|
|
|
|
191
|
|
|
var replaceEach = new RegExp(`<!-- \\[\\[${keys[i]}\\]\\][\\s\\S]*?-->`, 'g') |
192
|
|
|
template = template.replace(replaceEach, '') |
193
|
|
|
|
194
|
|
|
var patAttrSource = new RegExp(' ([A-Za-z0-9\-\_]+)=["|\'].*?({{' + keys[i] + '}}).*?["|\']', 'g') |
195
|
|
|
var patAttrSourceMatch = template.match(patAttrSource) |
196
|
|
|
|
197
|
|
|
if(patAttrSourceMatch != null) { |
198
|
|
|
let checkEscapedRegex = /["|'](.*?)["|']/ |
199
|
|
|
let patAttrSourceInside = new RegExp('(\\S+)=["\']?((?:.(?!["\']?\\s+(?:\\S+)=|[>"\']))+.)["\']?({{' + keys[i] + '}}).*?["|\']', 'g') |
200
|
|
|
Array.prototype.forEach.call(patAttrSourceMatch, (pat) => { |
201
|
|
|
let patAttrSourceCheck = patAttrSourceInside.exec(pat) |
202
|
|
|
if(patAttrSourceCheck != null) { |
203
|
|
|
|
204
|
|
|
let checkEscaped = checkEscapedRegex.exec(patAttrSourceCheck[0]) |
205
|
|
|
if(checkEscaped != null && checkEscaped.length > 0) { |
206
|
|
|
checkEscaped = escape(checkEscaped[1]) |
207
|
|
|
template = template.replace( |
|
|
|
|
208
|
|
|
patAttrSourceCheck[0], |
209
|
|
|
` data-abe-attr="${patAttrSourceCheck[1]}" data-abe-attr-escaped="${checkEscaped}" data-abe="${keys[i]}" ${patAttrSourceCheck[0]}` |
|
|
|
|
210
|
|
|
) |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
}) |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
var eachSource = new RegExp(`({{#each ${keys[i]}}[\\s\\S a-z]*?{{\/each}})`, 'g') |
217
|
|
|
var matches = template.match(eachSource) |
218
|
|
|
if(typeof matches !== 'undefined' && matches !== null) { |
219
|
|
|
Array.prototype.forEach.call(matches, (match) => { |
220
|
|
|
template = template.replace(match, `${match}<!-- [[${keys[i]}]] ${cmsTemplates.encodeAbeTagAsComment(match)} -->`) |
|
|
|
|
221
|
|
|
}) |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return template |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* THIS: |
231
|
|
|
<span>{{abe type='text' key='text_visible'}}</span> |
232
|
|
|
|
233
|
|
|
* BECOME: |
234
|
|
|
<span><abe>{{abe type='text' key='text_visible'}}</abe></span> |
235
|
|
|
|
236
|
|
|
* @param {[type]} template [description] |
237
|
|
|
*/ |
238
|
|
|
export function addAbeHtmlTagBetweenAbeTags(template) { |
239
|
|
|
var match |
240
|
|
|
var templateNoDom = striptags(template) |
241
|
|
|
while (match = cmsData.regex.abeAsTagPattern.exec(templateNoDom)) { |
242
|
|
|
template = template.replace(cmsData.regex.escapeTextToRegex(match[1], 'g'), '<abe>' + match[1].trim() + '</abe>') |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return template |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* THIS: |
250
|
|
|
[index]. |
251
|
|
|
|
252
|
|
|
* BECOME: |
253
|
|
|
{{@index}}- |
254
|
|
|
|
255
|
|
|
* @param {[type]} template [description] |
256
|
|
|
* @return {[type]} [description] |
257
|
|
|
*/ |
258
|
|
|
export function replaceAbeEachIndex(template) { |
259
|
|
|
return template.replace(/\[index\]\./g, '{{@index}}-') |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
export function removeHiddenAbeTag(template) { |
263
|
|
|
return template.replace(/(\{\{abe.*visible=[\'|\"]false.*\}\})/g, '') |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Remove {{abe type=*}} from html if attribute visible="false" |
268
|
|
|
* @param {[type]} template [description] |
269
|
|
|
* @return {[type]} [description] |
270
|
|
|
*/ |
271
|
|
|
export function removeHandlebarsRawFromHtml(template) { |
272
|
|
|
return template.replace(/\{\{\{\{\/?raw\}\}\}\}/g, '') |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* split {{#each}}...{{/each}} into an array |
277
|
|
|
* |
278
|
|
|
* @param {[type]} template [description] |
279
|
|
|
* @return {[type]} [description] |
280
|
|
|
*/ |
281
|
|
|
export function splitEachBlocks(template) { |
282
|
|
|
var block |
283
|
|
|
var blocks = [] |
284
|
|
|
|
285
|
|
|
while (block = cmsData.regex.blockPattern.exec(template)) { |
286
|
|
|
blocks.push(block[1]) |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return blocks |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
export function indexEachBlocks(template, onlyHtml) { |
293
|
|
|
// create an array of {{each}} blocks |
294
|
|
|
var blocks = cmsTemplates.prepare.splitEachBlocks(template) |
295
|
|
|
|
296
|
|
|
Array.prototype.forEach.call(blocks, (block) => { |
297
|
|
|
var key = block.match(/#each (.*)\}\}/)[1] |
298
|
|
|
var match |
299
|
|
|
|
300
|
|
|
if(!onlyHtml) { |
301
|
|
|
|
302
|
|
|
var voidData = {} |
303
|
|
|
voidData[key] = [{}] |
304
|
|
|
var blockCompiled = Handlebars.compile(block.replace(/{{abe (.*?["']) ?}}/g, '[[abe $1]]').replace(new RegExp(`\\.\\.\/${config.meta.name}`, 'g'), config.meta.name)) |
305
|
|
|
var blockHtml = blockCompiled(voidData, {data: {intl: config.intlData}}).replace(/\[\[abe (.*?)\]\]/g, '{{abe $1}}') |
306
|
|
|
|
307
|
|
|
// je rajoute un data-abe-block avec index sur tous les tags html du bloc each |
308
|
|
|
var textEachWithIndex = block.replace(/(<(?![\/])[A-Za-z0-9!-]*)/g, '$1 data-abe-block="' + key + '{{@index}}"') |
309
|
|
|
|
310
|
|
|
// je remplace le block dans le texte par ça |
311
|
|
|
template = template.replace(block, textEachWithIndex + `<!-- [[${key}]] ${cmsTemplates.encodeAbeTagAsComment(blockHtml)} -->`) |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
// Pour chaque tag Abe |
315
|
|
|
while (match = cmsData.regex.abeTag.exec(block)) { |
316
|
|
|
template = cmsTemplates.prepare.addAbeDictionnary(template, match[0], key) |
|
|
|
|
317
|
|
|
} |
318
|
|
|
}) |
319
|
|
|
|
320
|
|
|
return template |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* split {{#each}}...{{/each}} into an array |
325
|
|
|
* |
326
|
|
|
* THIS: |
327
|
|
|
{{abe type='text' key='test.title' desc='test title' tab='default'}} |
328
|
|
|
|
329
|
|
|
* BECOME THIS: |
330
|
|
|
{{abe dictionnary='test' type='text' key='test.title' desc='test title' tab='default'}} |
331
|
|
|
|
332
|
|
|
* |
333
|
|
|
* @param {[type]} template [description] |
334
|
|
|
* @return {[type]} [description] |
335
|
|
|
*/ |
336
|
|
|
export function addAbeDictionnary(template, match, key) { |
337
|
|
|
if(cmsData.regex.isEachStatement(match)) return |
|
|
|
|
338
|
|
|
|
339
|
|
|
if(cmsData.regex.isBlockAbe(match)){ |
340
|
|
|
var abeDictionnary = match.replace(new RegExp('(key=[\'|"])' + key + '.', 'g'), '$1' + key + '[index].') |
341
|
|
|
.replace(/\{\{abe/, '{{abe dictionnary=\'' + key + '\'') |
342
|
|
|
|
343
|
|
|
template = template.replace(match, abeDictionnary) |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return template |
347
|
|
|
} |
348
|
|
|
|